package com.campus.backend.controller;

import com.campus.backend.bean.coupon.CouponQueryParam;
import com.campus.backend.bean.coupon.CreateCouponBean;
import com.campus.backend.bean.coupon.UpdateCouponBean;
import com.campus.backend.vo.coupon.CouponVo;
import com.campus.backend.service.CouponService;
import com.campus.common.bean.common.Pagination;
import com.campus.common.bean.common.Resp;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 优惠券表 前端控制器
 * </p>
 *
 * @author ZhaoYuJie
 * @since 2025-05-21
 */
@Api(tags = "优惠券管理")
@RestController
@RequestMapping("/coupon")
public class CouponController {

    @Autowired
    private CouponService couponService;

    /**
     * 分页查询优惠券列表
     *
     * @param param 查询参数
     * @return 分页结果
     */
    @ApiOperation("分页查询优惠券列表")
    @GetMapping("/page")
    public Resp<Pagination<CouponVo>> page(@Validated CouponQueryParam param) {
        PageInfo<CouponVo> pageInfo = couponService.getCouponListPage(param);
        return Resp.successPageable(pageInfo);
    }

    /**
     * 获取优惠券详情
     *
     * @param couponId 优惠券ID
     * @return 优惠券详情
     */
    @ApiOperation("获取优惠券详情")
    @GetMapping("/{couponId}")
    public Resp<CouponVo> detail(@PathVariable Integer couponId) {
        CouponVo couponVo = couponService.getCouponById(couponId);
        return Resp.success(couponVo);
    }

    /**
     * 创建优惠券
     *
     * @param createCouponBean 优惠券信息
     * @return 创建结果
     */
    @ApiOperation("创建优惠券")
    @PostMapping
    public Resp<Integer> create(@RequestBody @Validated CreateCouponBean createCouponBean) {
        Integer couponId = couponService.createCoupon(createCouponBean);
        return Resp.success(couponId);
    }

    /**
     * 更新优惠券
     *
     * @param updateCouponBean 优惠券信息
     * @return 更新结果
     */
    @ApiOperation("更新优惠券")
    @PutMapping
    public Resp<String> update(@RequestBody @Validated UpdateCouponBean updateCouponBean) {
        couponService.updateCoupon(updateCouponBean);
        return Resp.success();
    }

    /**
     * 删除优惠券
     *
     * @param couponId 优惠券ID
     * @return 删除结果
     */
    @ApiOperation("删除优惠券")
    @DeleteMapping("/{couponId}")
    public Resp<String> delete(@PathVariable Integer couponId) {
        couponService.deleteCoupon(couponId);
        return Resp.success();
    }

    /**
     * 批量删除优惠券
     *
     * @param couponIds 优惠券ID列表
     * @return 删除结果
     */
    @ApiOperation("批量删除优惠券")
    @DeleteMapping("/batch")
    public Resp<String> batchDelete(@RequestBody List<Integer> couponIds) {
        couponService.batchDeleteCoupon(couponIds);
        return Resp.success();
    }
}
